package com.surfacing.rcp.client;

import java.util.ResourceBundle;


import org.apache.log4j.Logger;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.surfacing.rcp.client.listeners.AnforderungListener;
import com.surfacing.rcp.client.listeners.DienststelleListener;
import com.surfacing.rcp.client.listeners.DoctorListener;
import com.surfacing.rcp.client.listeners.EinsatzartListener;
import com.surfacing.rcp.client.listeners.JournalListener;
import com.surfacing.rcp.client.listeners.NacaListener;
import com.surfacing.rcp.client.listeners.NextProtokollNrListener;
import com.surfacing.rcp.client.listeners.NfsListener;
import com.surfacing.rcp.client.listeners.SessionListener;
import com.surfacing.rcp.client.listeners.StatAnforderungListener;
import com.surfacing.rcp.client.listeners.StatDienststelleListener;
import com.surfacing.rcp.client.listeners.StatDoctorListener;
import com.surfacing.rcp.client.listeners.StatEinsatzartListener;
import com.surfacing.rcp.client.listeners.StatNacaListener;
import com.surfacing.rcp.client.listeners.StatNfsListener;
import com.surfacing.rcp.client.listeners.StatTransportzielListener;
import com.surfacing.rcp.client.listeners.TransportzielListener;
import com.surfacing.rcp.client.listeners.UserListener;
import com.surfacing.rcp.client.modelManager.SessionManager;
import com.surfacing.rcp.core.net.NetWrapper;
import com.surfacing.rcp.model.entity.Anforderung;
import com.surfacing.rcp.model.entity.Dienststelle;
import com.surfacing.rcp.model.entity.Doctor;
import com.surfacing.rcp.model.entity.Einsatzart;
import com.surfacing.rcp.model.entity.Journal;
import com.surfacing.rcp.model.entity.Login;
import com.surfacing.rcp.model.entity.Logout;
import com.surfacing.rcp.model.entity.Naca;
import com.surfacing.rcp.model.entity.Nfs;
import com.surfacing.rcp.model.entity.ProtokollNr;
import com.surfacing.rcp.model.entity.StatAnforderung;
import com.surfacing.rcp.model.entity.StatDienststelle;
import com.surfacing.rcp.model.entity.StatDoctor;
import com.surfacing.rcp.model.entity.StatEinsatzart;
import com.surfacing.rcp.model.entity.StatNaca;
import com.surfacing.rcp.model.entity.StatNfs;
import com.surfacing.rcp.model.entity.StatTransportziel;
import com.surfacing.rcp.model.entity.SystemMessage;
import com.surfacing.rcp.model.entity.Transportziel;
import com.surfacing.rcp.model.entity.User;
import com.surfacing.rcp.model.factory.ImageFactory;
import com.surfacing.rcp.model.factory.ListenerFactory;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "com.surfacing.rcp.client"; //$NON-NLS-1$

	public static final String IMAGE_CLIENT_CONFIG_PATH = "com.surfacing.rcp.client.config.images";

	private static final Logger log = Logger.getLogger(Activator.class);

	// The shared instance
	private static Activator plugin;

	/**
	 * The constructor
	 */
	public Activator() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
	 * )
	 */
	@Override
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;

		// register the encoders and decoders
		NetWrapper.getDefault().registerEncoderAndDecoder();

		// load all needed images and register them
		loadAndRegisterImages();
		registerListeners();
	}

	/**
	 * Called when the plugin is stopped.
	 * 
	 * @param context
	 *            lifecyle informations
	 * @throws Exception
	 *             when a error occures during shutdown
	 */
	@Override
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given plug-in
	 * relative path
	 * 
	 * @param path
	 *            the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}

	private void registerListeners() {
		ListenerFactory factory = ListenerFactory.getDefault();
		// register the listeners
		factory.registerListener(Journal.ID, new JournalListener());
		factory.registerListener(Login.ID, new SessionListener());
		factory.registerListener(Logout.ID, new SessionListener());
		factory.registerListener(SessionManager.ID, new SessionListener());
		factory.registerListener(Nfs.ID, new NfsListener());
		factory.registerListener(Anforderung.ID, new AnforderungListener());
		factory.registerListener(Dienststelle.ID, new DienststelleListener());
		factory.registerListener(Doctor.ID, new DoctorListener());
		factory.registerListener(Einsatzart.ID, new EinsatzartListener());
		factory.registerListener(Naca.ID, new NacaListener());
		factory.registerListener(Transportziel.ID, new TransportzielListener());
		factory.registerListener(SystemMessage.ID, new SessionListener());
		factory.registerListener(User.ID, new UserListener());
		factory.registerListener(StatNaca.ID, new StatNacaListener());
		factory.registerListener(StatTransportziel.ID,
				new StatTransportzielListener());
		factory.registerListener(StatNfs.ID, new StatNfsListener());
		factory.registerListener(StatEinsatzart.ID,
				new StatEinsatzartListener());
		factory.registerListener(StatDoctor.ID, new StatDoctorListener());
		factory.registerListener(StatDienststelle.ID,
				new StatDienststelleListener());
		factory.registerListener(StatAnforderung.ID,
				new StatAnforderungListener());
		factory.registerListener(ProtokollNr.ID, new NextProtokollNrListener());

	}

	/**
	 * Loads all image files from the image.properties and registers them in the
	 * application.<br>
	 * The images can be accessed through the key value of the properties file.
	 */
	private void loadAndRegisterImages() {
		try {
			// the factory to register the images
			ImageFactory f = ImageFactory.getInstance();
			// open the properties file
			ResourceBundle imageBundle = ResourceBundle
					.getBundle(Activator.IMAGE_CLIENT_CONFIG_PATH);
			// loop and register all images
			for (String imageKey : imageBundle.keySet()) {
				// Create the image file with the given path
				String imagePath = imageBundle.getString(imageKey);
				ImageDescriptor imageDescriptor = AbstractUIPlugin
						.imageDescriptorFromPlugin(Activator.PLUGIN_ID,
								imagePath);
				f.registerImage(imageKey, imageDescriptor);
			}
		} catch (NullPointerException npe) {
			log.info("Please check the images and the properties file");
			log.info("Failed to load the images files");
			log.info("Please check the images and the properties file");
			log.error("NullPointer during Imageloading.", npe);
		}
	}

}
